package com.silence.gankio

import android.app.Activity
import android.app.Application
import android.os.Process
import com.alibaba.android.arouter.launcher.ARouter
import com.silence.gankio.core.app.Core
import com.xuexiang.rxutil2.logs.RxLog
import com.xuexiang.xui.XUI

class App : Application() {

    private val Instance by lazy { this }
    private val mActivitys by lazy { arrayListOf<Activity?>() }

    override fun onCreate() {
        super.onCreate()

        initARouter()
        initCore()
        initXUI()
        RxLog.debug(true)

    }

    private fun initARouter() {
        if (BuildConfig.DEBUG) {
            ARouter.openLog()
            ARouter.openDebug()
        }
        ARouter.init(this)
    }

    override fun onTerminate() {
        super.onTerminate()
        ARouter.getInstance().destroy()
    }

    private fun initXUI() {
        XUI.init(this)
        XUI.debug(true)

        initPage()

    }

    private fun initPage() {
    }


    private fun initCore() {
        Core.init(this)
            .withApiHost("http://gank.io/api/")
            .configure()
    }

    fun addActivity(activity: Activity) {
        mActivitys.add(activity)
    }

    fun exit() {
        for (activity in mActivitys) {
            if (null != activity) {
                activity.finish()
            }
        }
        //释放内存，退出程序
        Process.killProcess(Process.myPid())
    }

}